<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>二手房源管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="medium" style="margin-right: 400px"
                 @click="$router.push('/house/second-hand-house/add-new-1')">添加二手房源
      </el-button>
      <el-input placeholder="请输入标题或区域" v-model="searchText" class="input-with-select"
                style="width: 450px ;">
        <el-button slot="append" icon="el-icon-search" type="primary" @click="searchHouse">搜索</el-button>
      </el-input>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="房源名字"  align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="price" label="价格(元)" width="180" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="sectionName" label="地区" width="180" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column label="推荐" align="center" width="150">
        <template slot-scope="scope">
        <el-switch
            @change="toggleEnable(scope.row)"
            v-model="scope.row.recom"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
        </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="text-align: right; margin: 10px auto;" v-if="isDisplay">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>

    <el-dialog title="修改房源信息" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="editForm.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="editForm.area" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="推荐" prop="recom">
          <el-switch
              v-model="editForm.recom"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      searchText:'',
      editForm: {
        id: '',
        name: '',
        price: '',
        area: '',
        recom: ''
      },
      rules: {},
      isDisplay: true
    }
  },
  methods: {
    searchHouse() {
      let url = 'http://localhost:9200/house/list/cha?name=' + this.searchText+'&hcid=3';
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          this.isDisplay = false
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openEditDialog(house) {
      this.$router.push('/house/second-hand-house/update-1?id=' + house.id);
      //let url = 'http://localhost:9200/house/standard/second-hand-house?id=' + house.id;

      // console.log('url = ' + url);
      //
      // this.axios.get(url).then((response) => {
      //   let jsonResult = response.data;
      //   if (jsonResult.state == 20000) {
      //     this.editForm = jsonResult.data;
      //     this.dialogFormVisible = true;
      //   } else {
      //     this.$alert(jsonResult.message, '错误', {
      //       confirmButtonText: '确定',
      //       callback: action => {
      //         this.loadHouseList();
      //       }
      //     });
      //   }
      // });
    },
    submitEditForm() {
      let url = 'http://localhost:9200/house/update/second-hand-house';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改房源信息成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadHouseList();
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(house) {
      this.$confirm('此操作将永久删除【' + house.id + '-' + house.name + '】二手房源信息，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(house);
      }).catch(() => {
      });
    },
    handleDelete(house) {
      let url = 'http://localhost:9200/house/delete/second-hand-house';
      console.log('url = ' + url);

      let formData = 'id=' + house.id;
      console.log('formData = ' + formData);

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除二手房源成功！',
            type: 'success'
          });
          this.loadHouseList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadHouseList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadHouseList();
    },
    toggleEnable(house) {
      let enableText = ['不推荐', '推荐'];
      let url = 'http://localhost:9200/house/' + house.id;
      let enable = house.recom;
      if (enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);

      this.axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改为【' + enableText[enable] + '】成功！',
            type: 'success'
          });
          this.loadHouseList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadHouseList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadHouseList();
            }
          });
        }
      }).catch(err => {
        let message = '服务器响应错误！';
        this.$alert(message, '操作失败', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      });
    },
    loadHouseList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9200/house/list/second-hand-house?page=' + page;
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadHouseList();
  }
}
</script>